package com.seal.tsms.other.controller;

import com.seal.tsms.base.controller.BaseController;
import com.seal.tsms.base.dao.Pagination;
import com.seal.tsms.base.service.BaseService;
import com.seal.tsms.base.util.DateUtils;
import com.seal.tsms.other.entity.Subject;
import com.seal.tsms.other.entity.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by seal on 2016/3/20.
 */
@Controller
@RequestMapping("/other/subject")
public class SubjectController extends BaseController<Subject,String> {
    @Autowired
    @Qualifier("subjectServiceImpl")
    @Override
    public void setSelfService(BaseService<Subject, String> selfService) {
        super.setSelfService(selfService);
    }

    @RequestMapping(value = "/send",method = RequestMethod.POST)
    @ResponseBody
    public Map send(Subject subject, HttpSession session){
        Map data=new HashMap();

        subject.setCreateTime(DateUtils.getCurrentDate());
        subject.setToken((Token) session.getAttribute("token"));
        try {
            subject.setContent(HtmlUtils.htmlEscape(subject.getContent()));
            getSelfService().save(subject);
            data.put("state",true);
        }catch (Exception e){
            log.error("发布失败",e);
            data.put("state",false);
            data.put("msg","发布失败");

        }

        return data;
    }
    @RequestMapping(value = "/mine")
    public String mine(String pageIndexStr, HttpSession session, ModelMap modelMap){
        int pageIndex=1;
        if(pageIndexStr!=null&&pageIndexStr.matches("\\d+")){
            pageIndex=Integer.parseInt(pageIndexStr);
        }
        Pagination pagination=new Pagination();
        pagination.setIndex(pageIndex);
        pagination.setSize(12);
        Map condition=new HashMap();
        Map p=new HashMap();
        condition.put(Pagination.PARAMS,p);
        pagination.setCondition(condition);
        Token token= (Token) session.getAttribute("token");
        if(token!=null) {

            p.put("token", token.getId());

            condition.put(Pagination.RESULT_HQL, "query.subject_mine");
            getSelfService().browse(pagination);
            modelMap.addAttribute("pagination", pagination);

            return "other/mine";
        }else{
            return "redirect:/other/index.do";
        }

    }
    @RequestMapping(value = "/love")
    public String love(String pageIndexStr, HttpSession session, ModelMap modelMap){
        return  "redirect:/other/index.do";
    }
    @RequestMapping(value = "/reply")
    public String reply(String pageIndexStr, HttpSession session, ModelMap modelMap){
        return  "redirect:/other/index.do";
    }

}
